<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var a=[1,2,3,5,7,4]
    //非排序算法自己写的
    function f1(arr,count){
      let res = []
      while(res.length<count){
        let index = (Math.random() *  arr.length)>>0
        res.push(arr.splice(index,1)[0])
      }
    }
    //
    // arr.sort((a,b) => Math.random() - 0.5);
    //洗牌算法写法
    function getRandomArrElement(arr, count) {
      let newArr =[],
      index,
      temp,
      len = arr.length,
      min = len - count
      while(len > min){
        index = Math.floor((len--) * Math.random());
        temp=newArr[index]
        newArr[index] = newArr[len]
        newArr[len] = temp
      }
      return newArr.slice(min)
}

  console.log(getRandomArrElement(a,4))
  // console.log(a.shuffle(4))
  </script>
</body>
</html>